// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
#pragma once

#include <pollux/expression/complex_view_types.h>
#include <pollux/functions/udf.h>

namespace kumo::pollux::functions {
    template<typename TExec>
    struct MapKeyExists {
        POLLUX_DEFINE_FUNCTION_TYPES(TExec);

        void call(
            bool &out,
            const arg_type<Map<Generic<T1>, Generic<T2> > > &inputMap,
            const arg_type<Generic<T1> > &key) {
            out = (inputMap.find(key) != inputMap.end());
        }
    };

    template<typename TExec>
    struct MapRemoveNullValues {
        POLLUX_DEFINE_FUNCTION_TYPES(TExec);

        void call(
            out_type<Map<Generic<T1>, Generic<T2> > > &out,
            const arg_type<Map<Generic<T1>, Generic<T2> > > &inputMap) {
            for (const auto &entry: inputMap) {
                if (entry.second.has_value()) {
                    auto [keyWriter, valueWriter] = out.add_item();
                    keyWriter.copy_from(entry.first);
                    valueWriter.copy_from(entry.second.value());
                }
            }
        }
    };
} // namespace kumo::pollux::functions
